package com.casic.modules.jy.service.impl;

import com.casic.common.utils.Constant;
import com.casic.modules.jy.entity.JySjOptionEntity;
import com.casic.modules.jy.entity.JySjTitleEntity;
import com.casic.modules.jy.service.JyRklsService;
import com.casic.modules.jy.service.JySjOptionService;
import com.casic.modules.jy.service.JySjTitleService;
import com.casic.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.casic.common.base.service.impl.BaseServiceImpl;

import com.casic.modules.jy.dao.JySjInfoDao;
import com.casic.modules.jy.entity.JySjInfoEntity;
import com.casic.modules.jy.service.JySjInfoService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("jySjInfoService")
public class JySjInfoServiceImpl extends BaseServiceImpl<JySjInfoDao,JySjInfoEntity>  implements JySjInfoService {

    @Autowired
    private JySjTitleService jySjTitleService;
    @Autowired
    private JySjOptionService jySjOptionService;
    @Autowired
    public void setDao(JySjInfoDao jySjInfoDao) {
        this.dao=jySjInfoDao;
    }

    @Override
    public JySjInfoEntity copySave(JySjInfoEntity jySjInfo) {
        jySjInfo = this.dao.queryObject(jySjInfo.getId());
        JySjInfoEntity copyJySjInfo = new JySjInfoEntity();
        copyJySjInfo.setName(jySjInfo.getName()+"(副本)");
        String id=createId();
        copyJySjInfo.setId(id);
        copyJySjInfo.setCreateDate(new Date());
        SysUserEntity sysUserEntity= (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        copyJySjInfo.setCreateUserId(sysUserEntity.getUserId()+"");
        copyJySjInfo.setDeleteState(Constant.DELETE_STATE_NO);
        this.dao.save(copyJySjInfo);
        copyJySjInfo = findById(id);

        Map<String, Object> params = new HashMap<>();
        params.put("infoId", jySjInfo.getId());
        List<JySjTitleEntity> list = jySjTitleService.findList(params);
        if(list !=null && list.size()>0){
            for(JySjTitleEntity title : list){
                String copyTitleId = new String(title.getId());
                title.setInfoId(copyJySjInfo.getId());
                JySjTitleEntity copyTitle = jySjTitleService.save(title);
                params.put("titleCode",copyTitleId);
                List<JySjOptionEntity> optionList = jySjOptionService.findList(params);
                for(JySjOptionEntity o : optionList){
                    o.setTitleCode(copyTitle.getId());
                    jySjOptionService.save(o);
                }
            }
        }
        return copyJySjInfo;
    }
}
